﻿using CheckNoteWpf.API;
using CheckNoteWpf.Data;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace CheckNoteWpf
{
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 数据列表
        /// </summary>
        public List<InfoListItemData> InfoListItemDataList
        {
            get
            {
                if (infoListItemDataList == null)
                {
                    infoListItemDataList = LoadInfoListItemData();
                }
                if (infoListItemDataListDirty)
                {
                    infoListItemDataListDirty = false;
                    infoListItemDataList = new List<InfoListItemData>();
                    foreach (InfoListItem item in infoListItemList)
                    {
                        if (item.Data != null)
                        {
                            infoListItemDataList.Add(item.Data);
                        }
                    }
                }
                return infoListItemDataList;
            }
        }
        private List<InfoListItemData> infoListItemDataList;
        private bool infoListItemDataListDirty = false;

        /// <summary>
        /// 数据Item实体列表
        /// </summary>
        private List<InfoListItem> infoListItemList = new List<InfoListItem>();

        public MainWindow()
        {
            InitializeComponent();
            InitStyle();
            InitInfoList();
            Window_MouseLeave();
        }

        private void InitStyle()
        {
            Brush btnBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalDefine.GetConfigVal(ConfigKey.CommonBtnColorHex)));
            AddNewBtn.Background = btnBrush;
            AddNewBtn.BorderBrush = null;
            CloseBtn.Background = btnBrush;
            CloseBtn.BorderBrush = null;
            MinBtn.Background = btnBrush;
            MinBtn.BorderBrush = null;

            Brush btnTextBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(GlobalDefine.GetConfigVal(ConfigKey.CommonBtnTextColorHex)));
            AddNewBtn.Foreground = btnTextBrush;
            CloseBtn.Foreground = btnTextBrush;
            MinBtn.Foreground = btnTextBrush;
        }

        /// <summary>
        /// 初始化列表
        /// </summary>
        private void InitInfoList()
        {
            InfoListItemDataList.ForEach(data =>
            {
                CreateInfoListItem(data, false);
            });
        }

        /// <summary>
        /// 创建一条列表Item
        /// </summary>
        private InfoListItem CreateInfoListItem(InfoListItemData data = null, bool save = false)
        {
            InfoListItem item = new InfoListItem(data ?? InfoListItemData.Default);
            item.OnRemoveBtnClicked = RemoveInfoListItem;
            Action<InfoListItem> saveAction = (i) => SaveToInfoListItemData();
            item.OnTextBoxItemChanged = saveAction;
            item.OnTextBoxItemChanged = saveAction;

            infoListItemList.Add(item);
            InfoList.Children.Add(item);

            if (save) SaveToInfoListItemData();
            return item;
        }

        /// <summary>
        /// 移除一条列表Item
        /// </summary>
        /// <param name="item"></param>
        private void RemoveInfoListItem(InfoListItem item)
        {
            infoListItemDataListDirty = true;
            infoListItemList.Remove(item);
            InfoList.Children.Remove(item);
            SaveToInfoListItemData();
        }

        /// <summary>
        /// 加载列表数据
        /// </summary>
        private List<InfoListItemData> LoadInfoListItemData()
        {
            FileStream fs = File.Open(GlobalDefine.InfoListItemDataPath, FileMode.OpenOrCreate);
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(fs))
            {
                while (sr.Peek() != -1)
                {
                    sb.Append(sr.ReadLine());
                }
            }
            string data = sb.ToString();
            if (string.IsNullOrEmpty(data))
            {
                return new List<InfoListItemData>();
            }

            return JsonConvert.DeserializeObject<List<InfoListItemData>>(data);
        }

        /// <summary>
        /// 列表数据持久化
        /// </summary>
        private void SaveToInfoListItemData()
        {
            infoListItemDataListDirty = true;
            string data = JsonConvert.SerializeObject(InfoListItemDataList);
            if (string.IsNullOrEmpty(data))
            {
                return;
            }
            FileStream fs = File.Open(GlobalDefine.InfoListItemDataPath, FileMode.Create);
            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(data);
            }
        }

        /// <summary>
        /// 新增一条
        /// </summary>
        private void AddNewBtn_Click(object sender, RoutedEventArgs e)
        {
            CreateInfoListItem(null, true);
        }

        /// <summary>
        /// 关闭
        /// </summary>
        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            SaveToInfoListItemData();
            App.Close();
        }

        private void Window_MouseEnter(object sender = null, System.Windows.Input.MouseEventArgs e = null)
        {
            MainWin.Opacity = 1d;
            CloseBtn.Visibility = Visibility.Visible;
            AddNewBtn.Visibility = Visibility.Visible;
            MinBtn.Visibility = Visibility.Visible;
        }

        private void Window_MouseLeave(object sender = null, System.Windows.Input.MouseEventArgs e = null)
        {
            MainWin.Opacity = 0.8d;
            CloseBtn.Visibility = Visibility.Hidden;
            AddNewBtn.Visibility = Visibility.Hidden;
            MinBtn.Visibility = Visibility.Hidden;
        }

        private void MinBtn_Click(object sender, RoutedEventArgs e)
        {
            Hide();
        }
    }
}
